var SushiFileUpload = new Class({
	options: {
		uploader:'dynaupload.aspx',
		instances: '',
		fileformat: '',
		maxsize:4096
	},


	initialize: function(el,istanza,options) {
		
		this.Container = el;
		this.DivResponse =null;
		this.DivCommand =null;
		this.CommandClear =null;
		//this.HiddenValue =null;		
		this.IFrameUpd =null;
		this.PreviewUrl ="";	
		this.ClearUpload = this.clearUpload;
		this.options.instances = istanza;
		if(options) {
			
			this.options.fileformat = ((options.fileformat)?options.fileformat:'.gif,.jpg,.png,.tif,.bmp,.jpeg');
		}
		this.begin();
	},
	begin: function() {
		
		if(!this.Container) { return; }
		var conto = $(this.Container);
		conto.empty();
		conto.addClass('SushiUploadFile');
		this.IFrameUpd  = new Element('iframe');
		this.DivResponse = new Element('div');
		this.DivResponse.addClass('Preview');
		/*
		this.DivCommand = new Element('div');
		this.DivCommand.addClass('SUFBtm');		
		this.DivResponse.setHTML('loading');
		this.CommandClear = new Element('a');
		this.CommandClear.setHTML('clear');
		this.CommandSave = new Element('a');
		this.CommandSave.setHTML('save');		
		this.DivCommand.adopt( this.CommandClear );
		this.DivCommand.adopt( this.CommandSave );
		this.CommandClear.setStyle('display','none');
		*/
		//this.DivCommand.setStyle('display','none');		
		var postvar = "?source=" + this.options.instances; //this.Container;
		if(this.options.fileformat.toString()!="") {
			postvar+="&exts=" + escape(this.options.fileformat);	
		}
		

		this.iframeName = 'MioLiveEditor';
		this.IFrameUpd.setProperty('id',this.iframeName);
		this.IFrameUpd.setProperty('name',this.iframeName);
		this.IFrameUpd.setProperty('frameBorder',0);
		this.IFrameUpd.setProperty('scrolling','no');	
		this.IFrameUpd.setProperty('allowTransparency','yes');	
		this.IFrameUpd.setStyles( {
			'border':'0',
			'height':'35px',
			'width':'400px',
			'vertical-align':'top',
			'z-index':'75'															
			});			
		this.IFrameUpd.setProperty('src',this.options.uploader + postvar);
		conto.adopt(this.IFrameUpd);
		conto.adopt(this.DivResponse);
		//conto.adopt(this.DivCommand);		
	},
	clearUpload: function(fileme) {
			this.DivResponse.setStyle('display','none');
			if(!this.Container) { return; }
			var conto = $(this.Container);				
			var mio  = new Element('iframe');
			mio.setStyles( {
				'border':'0',
				'height':'35px',
				'width':'400px',
				'vertical-align':'top',
				'z-index':'75'															
				});		
			var postvar = "?ope=clear&key=" + fileme; //this.Container;
			mio.setProperty('src',this.options.uploader + postvar);							
			this.IFrameUpd.replaceWith(mio);
			this.IFrameUpd  = new Element('iframe');
			this.IFrameUpd.setStyles( {
				'border':'0',
				'height':'40px',
				'width':'400px',
				'vertical-align':'top',
				'z-index':'75'															
				});		
			postvar = "?source=" + this.options.instances; //this.Container;
			if(this.options.fileformat.toString()!="") {
				postvar+="&exts=" + escape(this.options.fileformat);	
			}
			this.IFrameUpd.setStyle('display','block');			
			this.IFrameUpd.setProperty('frameBorder',0);
			this.IFrameUpd.setProperty('scrolling','no');	
			this.IFrameUpd.setProperty('allowTransparency','yes');				
			this.IFrameUpd.setProperty('src',this.options.uploader + postvar);	
			$(this.Container).setProperty('value','');	
			this.PreviewUrl =		'';				
			mio.replaceWith(this.IFrameUpd);
		},
	beginUpload: function() {
		//during upload...
		var wP;
		wP = this.IFrameUpd.getPosition();	
		//this.IFrameUpd.setStyle('display','none');
		this.DivResponse.setStyles( {
			'display':'block',
			'position':'absolute',			
			'left':wP.x+'px',			
			'top':wP.y+'px',
			'z-index':'76'									
			});		
	},
	SavingEvent : function(guid,value,group,filename) {
		this.fireEvent('saving',[guid,'',group,filename,this]);
	},
	getUploaResponse : function (url,value,filename,filesize) {
		var fileme = url;
		var kbyte = parseInt(filesize);
		var bytetipe = "";
		
		if(kbyte>1048576) {
			kbyte = ((kbyte/1024)/1024).toFixed(2);
			bytetipe = "Mb";
		} else if( kbyte>1024 )  {
			kbyte = (kbyte/1024).toFixed(2) ;
			bytetipe = "kb" ;
		} else {
			bytetipe = "byte" ;
		}		
		
		$(this.Container).setProperty('value',value);	
		this.IFrameUpd.setStyle('display','none');
		this.DivResponse.setStyles( {
			'display':'block',
			'position':'relative',
			'left':'0px',			
			'top':'0px',						
			'z-index':'76',
			'width':'98%'
			});	
		var imgS = new Element('img');
		imgS.addClass('previewimg');
		this.PreviewUrl ='FileUploadHandler.ashx?k='+fileme+'&ope=p';	
		imgS.setProperty('src',	'FileUploadHandler.ashx?k='+fileme+'&ope=p');		
		//crea div info:
		var divInfo = new Element('div');
		divInfo.addClass('previewdinfo');
		
		var spanS = new Element('span');		
		spanS.addClass('previewspan');
		spanS.setHTML(filename + " (" + kbyte + " "+ bytetipe+")");
		//comando clear:
		var clearB = new Element('a').setStyle('display','block');
		clearB.setHTML('clear');
		divInfo.adopt(spanS);
		divInfo.adopt(clearB);

		var divGroup = new Element('div');
		divGroup.setStyles({'float':'left', 'margin-left':'3px' });
		var labelS = new Element('label');		
		labelS.setStyles({'font-size':'10px'});
		labelS.setProperty('for','groupname_upd');		
		labelS.setHTML('nome gruppo');
		var inputGrp = new Element('input').setProperty('type','text');		
		inputGrp.setProperty('id','groupname_upd');
		inputGrp.setStyles({'font-size':'10px','border':'1px solid #ccc'});
		//comando clear:
		var saveB = new Element('a').setStyle('display','block');
		saveB.setProperty('rel',filename);
		saveB.setHTML('save');
		
		divGroup.adopt(labelS);
		divGroup.adopt(inputGrp);		
		divGroup.adopt(saveB);


		this.DivResponse.empty();
		this.DivResponse.adopt(imgS);
		this.DivResponse.adopt(divInfo);
		this.DivResponse.adopt(divGroup);		
		var mioFF = this;	

		saveB.addEvent('click',function() {	
			var gruppoF = $('groupname_upd');
			var gruppoT = "";
			if(gruppoF)
				gruppoT = gruppoF.getProperty('value');
			
			//if(!mioFF.Container) { alert('sono qui'); return; }
			//var conto = $(mioFF.Container);					
			//var valore = conto.getProperty('value');
			mioFF.SavingEvent(fileme,'',gruppoT,this.getProperty('rel'));
			//alert('gg: ' + gruppoT + '\nValue:\n' + valore);
		});
		
		clearB.addEvent('click',function() {
				mioFF.clearUpload(fileme);
			});
	}
});
SushiFileUpload.implement(new Options, new Events);
